package com.llxwz.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llxwz.dao.CommentDao;
import com.llxwz.entity.Comment;
import com.llxwz.util.CommunityConstant;
import com.llxwz.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class CommentService implements CommunityConstant {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private DiscussPostService discussPostService;

    public List<Comment> findCommentsByEntityType(int entityType, int entityId, int current, int limit){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("entity_type",entityType);
        wrapper.eq("entity_id",entityId);
        wrapper.eq("status",0);
        wrapper.orderByAsc("create_time");
        Page result = commentDao.selectPage(new Page<>(current, limit), wrapper);
        return result.getRecords();
    }

    //评论数量
    public int findCountByEntity(int entityType,int entityId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("entity_type",entityType);
        wrapper.eq("entity_id",entityId);
        wrapper.eq("status",0);

        return commentDao.selectCount(wrapper);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public int addComment(Comment comment){
        if(comment==null){
            throw new IllegalArgumentException("参数不能为空");
        }
        //添加评论
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        int rows = commentDao.insert(comment);

        //更新帖子的评论数量
        if (comment.getEntityType() == ENTITY_TYPE_POST){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("entity_type",ENTITY_TYPE_POST);
            wrapper.eq("entity_id",comment.getEntityId());
            wrapper.eq("status",0);
            int count = commentDao.selectCount(wrapper);
            discussPostService.updateCommentCount(comment.getEntityId(),count);
        }

        return rows;
    }

    public Comment findCommentById(int id){
        return commentDao.selectById(id);
    }

    public int findCountByUserId(int entityType,int userId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("entity_type",entityType);
        wrapper.eq("user_id",userId);
        return commentDao.selectCount(wrapper);
    }

    public List<Comment> findCommentsByUserId(int entityType,int userId,int current, int limit){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("entity_type",entityType);
        wrapper.eq("user_id",userId);
        wrapper.eq("status",0);
        wrapper.orderByAsc("create_time");
        Page result = commentDao.selectPage(new Page<>(current, limit), wrapper);
        return result.getRecords();
    }

}
